package spring.cloud.tasks.tasks_manager.service;

import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.curator.Curator;
import spring.cloud.tasks.common.curator.CuratorFactory;
import spring.cloud.tasks.common.curator.CuratorInfo;
import spring.cloud.tasks.common.nodepaths.TaskNodePaths;
import spring.cloud.tasks.tasks_manager.domain.ZooKeeperTaskConfiguration;
import spring.cloud.tasks.tasks_manager.dto.NamespaceDto;
import spring.cloud.tasks.tasks_manager.dto.ZooKeeperDto;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TaskService {


    @Resource
    private ZooKeeperDtoService zooKeeperClusterDtoService;
    @Resource
    private NamespaceDtoService nameSpaceDtoService;

    @Resource
    private TaskDtoService taskConfigurationDtoService;
    @Resource
    private CuratorFactory zooKeeperRepository;


    public List<String> getTaskIdList(String nameSpace) throws RuntimeException {
        List<String> taskIdList = taskConfigurationDtoService.getTaskIdListBynameSpace(nameSpace);
        return taskIdList != null ? taskIdList : new ArrayList<>();
    }

    /**
     * 返回的taskId有的可能是正常状态的 有的可能是非正常状态的
     *
     * @param nameSpace
     * @return
     */
    public List<ZooKeeperTaskConfiguration> getTaskConfigurationListFromZooKeeper(String nameSpace) {
        NamespaceDto namespaceDto = nameSpaceDtoService.getNamespaceDto(nameSpace);
        ZooKeeperDto zooKeeperDto = zooKeeperClusterDtoService.selectByZooKeeperKey(namespaceDto.getZookeeperKey());
        CuratorInfo curatorInfo = new CuratorInfo();
        curatorInfo.setAddressList(zooKeeperDto.getAddressList());
        curatorInfo.setDigest(zooKeeperDto.getDigest());
        curatorInfo.setNamespaceKey(nameSpace);
        Curator curator = CuratorFactory.getCurator(curatorInfo, CuratorFactory.shortTime_true);
        List<String> taskIdList = curator.getChildren(TaskNodePaths.ROOT);
        List<ZooKeeperTaskConfiguration> zooKeeperTaskConfigurationList = new ArrayList<ZooKeeperTaskConfiguration>();
        for (String taskId : taskIdList) {
            //
        }
        return zooKeeperTaskConfigurationList;
    }


}
